/*
 * @Description: 
 * @Version: 2.0
 * @Autor: ftt
 * @Date: 2022-06-10 14:15:31
 * @LastEditors: TT
 * @LastEditTime: 2022-06-20 15:34:13
 */

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import 'package:sp_util/sp_util.dart';
import 'package:ttcomment/comment_http/default_http_transformer.dart';
import '../../utils/common_sp_util.dart';
import '../../utils/utils_tool.dart';
import '../controller/user_state_controller.dart';
import '../models/user_model.dart';
import '../transformers/normal_interceptor.dart';
import 'http_normal_config.dart';

class GloblConfig {
  /// 初始化
  static init() async {
    /// 初始化生命周期 监听
    WidgetsFlutterBinding.ensureInitialized();
    await initthirdparty();
    await initNetwork();
  }

  /// 初始化第三方库
  static initthirdparty() async {
    /// 初始化本地数据库
    await SpUtil.getInstance();

    /// 配置通用 loading 时长
    EasyLoading.instance.displayDuration = const Duration(
      milliseconds: 1500,
    );
  }

  /// 初始化默认数据
  static initnormaldata() async {
    if (UtilsTool.userislogin()) {
      UserModel model = UserModel.fromMap(CommonSpUtil.getUserInfo()!);
      UserStateController.current.loadSuccess(model);
    }
  }

  /// 配置网络请求
  static initNetwork() async {
    /// 配置 网络请求 解析模型
    ///{
    ///  "Success":true,
    ///   "Stamp":"",
    ///   "Message":"",
    ///   "Data":""
    ///}
    DefaultHttpTransformer.getInstance().setHttpConfig(
      config: NormalHttpConfig(
        status: "Success",
        code: "Stamp",
        msg: "Message",
        data: "Data",
      ),
    );

    /// 通过 getx 注入网络请求

    Get.put(
      HttpClient(
        dioConfig: HttpConfig(
          /// 配置通用域名
          baseUrl: HttpNormalConfig.baseUrl,

          /// 配置 项目 请求头
          interceptors: [
            NormalInterceptor(),
          ],
        ),
      ),

      /// 全局共享
      permanent: true,

      /// 标识
      tag: HttpClient.defaultClientTag,
    );
  }
}
